package com.iplay.实战java高并发程序设计.chapter5.c55;

/**
 * FutureData 是RealData 的包装
 * @author dangwu
 * @since 2021/11/5 4:43 下午
 */
public class FutureData implements Data{
    protected RealData realData = null;
    protected boolean isReady = false;
    public synchronized void setRealData(RealData realData){
        if (isReady){
            return;
        }
        this.realData = realData;
        isReady = true;
        //RealData已经被注入，通知getResult()方法
        notifyAll();
    }

    @Override
    public synchronized String getResult() {//等待RealData构造完成
        while (!isReady){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return realData.result;
    }
}
